library MarineCombatKnife initializer init requires LegacySystem

	private function CombatKnifePerserveInvisible takes nothing returns boolean
		if GetIssuedOrderId() == ORDER.ancestralspirit and GetUnitAbilityLevel(GetTriggerUnit(), 'A092') > 0 then
			call PerserveInvisibleOnSpellAction(GetTriggerUnit())
		endif
		return false
	endfunction
	
	private function CombatKnifeConditions takes nothing returns boolean
		return GetSpellAbilityId() == 'A092' 
	endfunction
	
	private function KnifeActions takes nothing returns nothing
		local unit u = GetTriggerUnit()
		local player p = GetOwningPlayer(u)
		local integer alvl = GetUnitAbilityLevel(u,'A092')

		if alvl == 1 then
			set bj_meleeNearestMineDist = GetUnitX(u)
			set bj_lastTransmissionDuration = GetUnitY(u)
			set udg_soundpath = "war3mapImported\\CombatKnife.wav"
			call PlaySoundPath3D.execute()
			// off -> on
			call SetUnitAbilityLevel(u, 'A092', 2)
			call UnitAddAbility(u,'Abun')
			call UnitAddAbility(u,'A04X')
			call DisplayTimedTextToPlayer(p,0.0,0.0,2.0, "|cff828250Switching to combat knife|r" )
			call ShiftMoveSpeed(u,12.0)
		elseif alvl == 2 then
			set bj_meleeNearestMineDist = GetUnitX(u)
			set bj_lastTransmissionDuration = GetUnitY(u)
			set udg_soundpath = "war3mapImported\\CombatKnife.wav"
			call PlaySoundPath3D.execute()
			
			call SetUnitAbilityLevel(u, 'A092', 1)
			// on - > off 
			call UnitRemoveAbility(u,'Abun')
			call UnitRemoveAbility(u,'A04X')
			call DisplayTimedTextToPlayer(p,0.0,0.0,2.0, "|cff828250Switching to primary weapon|r" )
			call ShiftMoveSpeed(u,-12.0)	
		endif
		
		set u = null
		set p = null
	endfunction

	
	private function init takes nothing returns nothing
		local trigger t

		set t = CreateTrigger()
		
		call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_ISSUED_ORDER )
		call TriggerAddCondition(t,Condition(function CombatKnifePerserveInvisible))
		
		set t =  CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_SPELL_EFFECT  )
		call TriggerAddCondition(t,Condition(function CombatKnifeConditions))
		call TriggerAddAction(t,function KnifeActions)
		
		set t = null
	endfunction

endlibrary